package wbhproject.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import wbhproject.tools.MeasurePointRequestorController;

/**
 * Author: Thomas Aulich
 * Datum: 
 * HttpServlet implementation class test (just...)
 */
public class Test extends HttpServlet {
	private static final long serialVersionUID = 1L;
	static Logger log = Logger.getLogger(Test.class);

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		log.debug("service was called");
		doBasicRequest(request, response);
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		log.debug("doGet was called");
		doBasicRequest(request, response);
	}
	
	/**
	 * Method created for a unified response from service and doGet...
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void doBasicRequest(HttpServletRequest request,
	HttpServletResponse response) throws ServletException, IOException {
		log.debug("doBasicRequest was called");
		MeasurePointRequestorController myMPRController = MeasurePointRequestorController
				.getInstance();
		String mode = request.getParameter("mode");
		PrintWriter out = response.getWriter();
		response.setContentType("text/html");
		out.print("<html><body>");
		out.print("<h1>Response from servlet 'Test':</h1>");
		out.print("<p>mode-parameter is: " + mode + "</p>");
		if (mode != null) {
			if (mode.equals("stoprequestor")){
				out.print("<p>MeasurePointRequestorController-running-before: " + MeasurePointRequestorController.requestorRunning() + "</p>");
				myMPRController.requestorStop();
				out.print("<p>MeasurePointRequestorController-running-after: " + MeasurePointRequestorController.requestorRunning() + "</p>");
			}
			if (mode.equals("startrequestor")){
				out.print("<p>MeasurePointRequestorController-running-before: " + MeasurePointRequestorController.requestorRunning() + "</p>");
				myMPRController.requestorStart();
				out.print("<p>MeasurePointRequestorController-running-after: " + MeasurePointRequestorController.requestorRunning() + "</p>");
			}			
		}
		out.print("<a href='index.jsp'>back</a>");
		out.print("</table></body></html>");
	}
}